<?php

class PublicController extends Controller
{
	public  $jsHightLight = '<script type="text/javascript" src="../../../themes/taskme/js/hight.light.cms.js"></script>';
	public function find_all_files($dir) 
	{ 
		$root = scandir($dir); 
		
		$result = array();
		foreach($root as $value) 
		{ 
			if($value === '.' || $value === '..') {continue;} 
			if(is_file("$dir/$value")) {$result[]="$value";continue;} 
			foreach($this->find_all_files("$dir/$value") as $value) 
			{ 
				$result[]=$value; 
			} 
		} 
		return $result; 
	}
	function getTextBetweenTags($tag, $html, $strict=0)
	{
		/*** a new dom object ***/
		$dom = new domDocument;

		/*** load the html into the object ***/
		if($strict==1)
		{
			$dom->loadXML($html);
		}
		else
		{
			$dom->loadHTML($html);
		}

		/*** discard white space ***/
		$dom->preserveWhiteSpace = false;

		/*** the tag by its tag name ***/
		$content = $dom->getElementsByTagname($tag);

		/*** the array to return ***/
		$out = array();
		foreach ($content as $item)
		{
			/*** add node value to the out array ***/
			$out[] = $item->nodeValue;
		}
		/*** return the results ***/
		return $out;
	}
	public function actionApplyMode(){
		$name = Yii::app()->request->getParam('name',null);
		$structure = 'projects/'.outStr(currentUser(true)->id)."/sunhouse/".$name;
		
		try{
		
			$file = file_get_contents($structure, FILE_USE_INCLUDE_PATH);
			
			$f = str_replace($this->jsHightLight,'',$file);
			$f = str_replace("</body>","	".$this->jsHightLight."
</body>",$f);
			@file_put_contents($structure, $f);
			
			jsonOut(array(
				'error'=>false,
				'message'=>'Add JS successfully.'
			));
			
		}catch(Exception $e){
			jsonOut(array(
				'error'=>true,
				'message'=>$e->getMessage()
			));
		}
		
		
	}
	public function actionCreateLayout(){
		if(!empty($_POST)){
			$html = $_POST['html'];
			$html = str_replace($this->jsHightLight,"",$html);
			$html = str_replace('php echo $content','<?php echo $content;?>',$html);
			$structure = 'projects/source/'.outStr(currentUser(true)->id)."/sunhouse/protected/views/layouts/main.php";
			
			
			$explode = explode("\n",$html);
			foreach($explode as $key=>$row){
				$pos = strpos($row,"link");
				if($pos === false){
					
				}else{
					
					$attr = $this->getAttribute("href",$row);
					$new_attr = "<?php echo Yii::app()->baseUrl;?>/lib/".$attr;
					$new_row = str_replace($attr,$new_attr,$row);
					$html = str_replace($row,$new_row,$html);
				}
				$pos = strpos($row,'<script type="text/javascript" src');
				if($pos === false){
					
				}else{
					
					$attr = $this->getAttribute("src",$row);
					$new_attr = "<?php echo Yii::app()->baseUrl;?>/lib/".$attr;
					$new_row = str_replace($attr,$new_attr,$row);
					$html = str_replace($row,$new_row,$html);
				}
			}
			
			@file_put_contents($structure, $html);
			
			
			jsonOut(array(
				'error'=>false,
				'message'=>'Success',
				'html'=>$html
			));
		}else{
			jsonOut(array(
				'error'=>true,
				'message'=>'Request bad'
			));
		}
	}
	
	/**
	 * This function used get attribute html
	 * 
	 **/
	
	function getAttribute($attrib, $tag){
		//get attribute from html tag
		$re = '/'.$attrib.'=["\']?([^"\' ]*)["\' ]/is';
		preg_match($re, $tag, $match);
		if($match){
			return urldecode($match[1]);
		}else {
			return false;
		}
	}
	
	public function actionIndex()
	{
		dump('sfdsf');
	}
}